package com.tcoiss.datafactory.config;

import com.tcoiss.common.datasource.DynamicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.Map;

@Component
public class DynamicJdbcTemplate {

    private String dbName;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private DynamicDataSource dynamicDataSource;

    public JdbcTemplate getJdbcTemplate(String dbName) {
        Map<Object, Object> dataSources = dynamicDataSource.getDynamicTargetDataSources();
        jdbcTemplate.setDataSource((DataSource) dataSources.get(dbName));
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }
}
